﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;
using DevExpress.XtraReports.UI;
using DevExpress.XtraReports.UserDesigner;

namespace BHair.Base
{
    public partial class frmxs : Form
    {
        public frmxs()
        {
            InitializeComponent();
        }
        string constr = ConfigurationManager.ConnectionStrings["BHair.Properties.Settings.BHairDBConnectionString"].ConnectionString;
        string siweils = "";
        private void frmxs_Load(object sender, EventArgs e)
        {
            this.txtcardid.Focus();
            djh();
            showfenlei();
        }
        //显示商品分类
        public void showfenlei()
        {
            SqlConnection conn = new SqlConnection(constr);
            try
            {
                conn.Open();
                string sql = "SELECT [ID],[type]as 分类 FROM [BHairDB].[dbo].[商品分类]";
                SqlDataAdapter sda = new SqlDataAdapter(sql,conn);
                DataTable dt = new DataTable();
                sda.Fill(dt);
                dgvfenlei.DataSource = dt;
                conn.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        public void showsplist()
        {
            //选中的分类编号
            int typeId=Int32.Parse( dgvfenlei.SelectedCells[0].Value.ToString());
            //根据分类编号查询并显示商品
           
                string sql = string.Format("SELECT [iid],[iname] as 商品名称,[iuprice] as 售价 FROM [items] where itype={0}", typeId);
                SqlConnection conn = new SqlConnection(constr);
                conn.Open();
                SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
                DataTable dt = new DataTable();
                sda.Fill(dt);
                dgvsplist.DataSource = dt;
                dgvsplist.Columns[0].Visible = false;
           
            //else if (lbyhfs.Text.Trim() == "折扣")
            //{

            //}
            //else if (lbyhfs.Text.Trim() == "会员价一")
            //{
            //    string sql = string.Format("SELECT [iid],[iname] as 商品名称,[iuprice] as 原价,[hyj1] as 现价 FROM [items] where itype={0}", typeId);
            //    SqlConnection conn = new SqlConnection(constr);
            //    conn.Open();
            //    SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
            //    DataTable dt = new DataTable();
            //    sda.Fill(dt);
            //    dgvsplist.DataSource = dt;
            //    dgvsplist.Columns[0].Visible = false;
            //}
            //else if (lbyhfs.Text.Trim() == "会员价二")
            //{
            //    string sql = string.Format("SELECT [iid],[iname] as 商品名称,[iuprice] as 原价,[hyj2] as 现价 FROM [items] where itype={0}", typeId);
            //    SqlConnection conn = new SqlConnection(constr);
            //    conn.Open();
            //    SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
            //    DataTable dt = new DataTable();
            //    sda.Fill(dt);
            //    dgvsplist.DataSource = dt;
            //    dgvsplist.Columns[0].Visible = false;
            //}
            //else if (lbyhfs.Text.Trim() == "会员价三")
            //{
            //    string sql = string.Format("SELECT [iid],[iname] as 商品名称,[iuprice] as 原价,[hyj3] as 现价 FROM [items] where itype={0}", typeId);
            //    SqlConnection conn = new SqlConnection(constr);
            //    conn.Open();
            //    SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
            //    DataTable dt = new DataTable();
            //    sda.Fill(dt);
            //    dgvsplist.DataSource = dt;
            //    dgvsplist.Columns[0].Visible = false;
            //}
        }
        private void dgvfenlei_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            showsplist();
        }
        private bool isinsert(string key)
        {

            int cout = 0;
            for (int i = 0; i < dgvxsqd.Rows.Count; i++)
            {
                if (key.Equals(dgvxsqd["key", i].FormattedValue.ToString()))
                {
                    cout++;

                }
            }
            if (cout > 1)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        //双击选择商品
        private void dgvsplist_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            DataGridView dgv = dgvxsqd;
            //判断是否已经有此消费
            foreach (DataGridViewRow v in dgvxsqd.Rows)
            {
                if (v.Cells[0].Value != null)
                {
                    var count = 0;
                    foreach (DataGridViewRow v2 in dgvxsqd.Rows)
                    {
                        if (v2.Cells[0].Value != null)
                        {
                            if (v.Cells[0].Value.ToString().Equals(dgvsplist.SelectedCells[1].Value.ToString()))
                                count++;
                        }
                    }
                    if (count > 0)
                    {
                        //如果重复则数量加1
                        // MessageBox.Show("第1列有重复，重复的内容是:【" + v.Cells[0].Value + "】");
                        //DataTable rentTable = (DataTable)dgvxsqd.;//获取数据源

                       
                            for (int i = 0; i < dgvxsqd.Rows.Count; i++)
                            {
                                if (dgvxsqd.Rows[i].Cells[0].Value.ToString() == dgvsplist.SelectedCells[1].Value.ToString())
                                {
                                    this.dgvxsqd.Rows[i].Cells[3].Value = decimal.Parse(this.dgvxsqd.Rows[i].Cells[3].Value.ToString()) + 1;
                                this.dgvxsqd.Rows[i].Cells[4].Value = decimal.Parse(this.dgvxsqd.Rows[i].Cells[1].Value.ToString()) * decimal.Parse(this.dgvxsqd.Rows[i].Cells[3].Value.ToString());
                               // DataGridView dgv = dgvxsqd;
                                Total(dgv);
                                //dataGridView1.Rows[i].Selected = true;
                                //dataGridView1.FirstDisplayedScrollingRowIndex = i;
                                return;
                                }
                            }




                         
                    }
                }
            }
           
            int index = this.dgvxsqd.Rows.Add();
            this.dgvxsqd.Rows[index].Cells[0].Value = dgvsplist.SelectedCells[1].Value.ToString();
            this.dgvxsqd.Rows[index].Cells[1].Value = dgvsplist.SelectedCells[2].Value.ToString();
            this.dgvxsqd.Rows[index].Cells[2].Value = dgvsplist.SelectedCells[2].Value.ToString();
            this.dgvxsqd.Rows[index].Cells[3].Value = "1";
            this.dgvxsqd.Rows[index].Cells[4].Value= decimal.Parse(this.dgvxsqd.Rows[index].Cells[1].Value.ToString()) * decimal.Parse(this.dgvxsqd.Rows[index].Cells[3].Value.ToString());
           
            Total(dgv);


            //chuanzhi cz = new chuanzhi();

            //if (txtcardid.Text == string.Empty)
            //{
            //    MessageBox.Show("请先刷卡");
            //    txtcardid.Focus();
            //}
            //else
            //{
            //    //判断当前会员的卡类别的优惠方式，折扣，会员价123

            //    if (cz.shifoudazhe(dgvsplist.SelectedCells[0].Value.ToString()) == true)
            //    {
            //        int index = this.dgvxsqd.Rows.Add();
            //        this.dgvxsqd.Rows[index].Cells[0].Value = dgvsplist.SelectedCells[1].Value.ToString();

            //        this.dgvxsqd.Rows[index].Cells[1].Value = decimal.Parse(dgvsplist.SelectedCells[2].Value.ToString()) * decimal.Parse(lbzl.Text);
            //        this.dgvxsqd.Rows[index].Cells[2].Value = "1";
            //        this.dgvxsqd.Rows[index].Cells[3].Value = decimal.Parse(this.dgvxsqd.Rows[index].Cells[2].Value.ToString()) * decimal.Parse(this.dgvxsqd.Rows[index].Cells[1].Value.ToString());
            //        DataGridView dgv = dgvxsqd;
            //        Total(dgv);
            //    }
            //    else if(cz.shifoudazhe(dgvsplist.SelectedCells[0].Value.ToString())==false)
            //    {
            //        int index = this.dgvxsqd.Rows.Add();
            //        this.dgvxsqd.Rows[index].Cells[0].Value = dgvsplist.SelectedCells[1].Value.ToString();

            //        this.dgvxsqd.Rows[index].Cells[1].Value = decimal.Parse(dgvsplist.SelectedCells[2].Value.ToString());
            //        this.dgvxsqd.Rows[index].Cells[2].Value = "1";
            //        this.dgvxsqd.Rows[index].Cells[3].Value = decimal.Parse(this.dgvxsqd.Rows[index].Cells[2].Value.ToString()) * decimal.Parse(this.dgvxsqd.Rows[index].Cells[1].Value.ToString());
            //        DataGridView dgv = dgvxsqd;
            //        Total(dgv);
            //    }
            //}
        }
        public void Total(DataGridView dg)
        {
            // DataGridViewRow dgr = dg.Rows[dg.Rows.Count];
            decimal sum = 0;
            for (int i = 0; i < dg.Rows.Count; i++)
            {
                sum += Convert.ToDecimal(Convert.ToSingle(dg.Rows[i].Cells[4].Value));
            }
            this.lbheji.Text = sum.ToString();
        }
        //重新计算小计
        public void js()
        {
            for (int i=0;i<dgvxsqd.Rows.Count;i++)
            {
                dgvxsqd.Rows[i].Cells["Column4"].Value = decimal.Parse(dgvxsqd.Rows[i].Cells["Column3"].Value.ToString()) * decimal.Parse(dgvxsqd.Rows[i].Cells["Column2"].Value.ToString());
            }
            DataGridView dgv = dgvxsqd;
            Total(dgv);
        }
        //修改数量

        private void button2_Click(object sender, EventArgs e)
        {
            //打开修改数量窗口
            frmsl sl = new frmsl();
            sl.ShowDialog();
            //修改选中行的数量
            int a = dgvxsqd.CurrentRow.Index;
            dgvxsqd.Rows[a].Cells["Column3"].Value = chuanzhi.xssl;
            //修改选中行的小计
            dgvxsqd.Rows[a].Cells["Column4"].Value = decimal.Parse(dgvxsqd.Rows[a].Cells["Column3"].Value.ToString()) * decimal.Parse(dgvxsqd.Rows[a].Cells["Column2"].Value.ToString());
            //修改合计
            DataGridView dgv = dgvxsqd;
            Total(dgv);

        }
        //删除选中行
        private void button6_Click(object sender, EventArgs e)
        {
            //判断是否有选中行
            /*if (this.dgvxsqd.SelectedRows.Count > 0)
            {
                DataRowView drv = dgvxsqd.SelectedRows[0].DataBoundItem as DataRowView;
                drv.Delete();
            }
            */
            foreach (DataGridViewRow r in dgvxsqd.SelectedRows)//选中的行
            {
                this.dgvxsqd.Rows.Remove(r);
            }
            DataGridView dgv = dgvxsqd;
            Total(dgv);

        }
        //读取会员信息
        public void insertxsbak()
        {
            SqlConnection conn = new SqlConnection(constr);
            conn.Open();
            for (int i=0;i<dgvxsqd.Rows.Count;i++)
            {
                string sqlstr = string.Format("INSERT INTO [xsbak]([djh] ,[商品名称] ,[原价] ,[现价] ,[数量] ,[小计],jsj) VALUES ('{0}'  ,'{1}'  ,'{2}'  ,'{3}'  ,'{4}' ,'{5}','{6}')",txtdjh.Text, dgvxsqd.Rows[i].Cells[0].Value, dgvxsqd.Rows[i].Cells[1].Value, dgvxsqd.Rows[i].Cells[2].Value, dgvxsqd.Rows[i].Cells[3].Value,dgvxsqd.Rows[i].Cells[4].Value, GetComputerName());
                SqlCommand comm = new SqlCommand(sqlstr,conn);
                comm.ExecuteNonQuery();
            }
            conn.Close();
        }
        public void upzk()
        {
            //先根据名称查找 对应的价格进行修改现价
            if (lbyhfs.Text.Trim() == "会员价一")
            {
                SqlConnection conn = new SqlConnection(constr);
                conn.Open();
                SqlConnection conn1 = new SqlConnection(constr);
                conn1.Open();
                for (int i = 0; i < dgvxsqd.Rows.Count; i++)
                {
                    decimal hyj1 = 0;//会员价1
                    string sqlstr = string.Format("select iname,hyj1,hyj2,hyj3 from items where iname='{0}'", dgvxsqd.Rows[i].Cells[0].Value);
                    SqlCommand comm = new SqlCommand(sqlstr, conn);
                    SqlDataReader sdr = comm.ExecuteReader();
                    while (sdr.Read())
                    {
                        hyj1 = decimal.Parse(sdr["hyj1"].ToString());
                    }
                    //修改这个单据的这个商品现价为hyj1
                    string sql1 = string.Format("UPDATE [xsbak] SET [现价] = '{0}' WHERE djh='{1}' and 商品名称='{2}'", hyj1, txtdjh.Text, dgvxsqd.Rows[i].Cells[0].Value);
                    SqlCommand comm1 = new SqlCommand(sql1, conn1);
                    comm1.ExecuteNonQuery();
                    sdr.Close();
                }

                conn.Close();
                conn1.Close();
            }
            else if (lbyhfs.Text.Trim() == "会员价二")
            {
                SqlConnection conn = new SqlConnection(constr);
                conn.Open();
                SqlConnection conn1 = new SqlConnection(constr);
                conn1.Open();
                for (int i = 0; i < dgvxsqd.Rows.Count; i++)
                {
                    decimal hyj1 = 0;//会员价1
                    string sqlstr = string.Format("select iname,hyj1,hyj2,hyj3 from items where iname='{0}'", dgvxsqd.Rows[i].Cells[0].Value);
                    SqlCommand comm = new SqlCommand(sqlstr, conn);
                    SqlDataReader sdr = comm.ExecuteReader();
                    while (sdr.Read())
                    {
                        hyj1 = decimal.Parse(sdr["hyj2"].ToString());
                    }
                    //修改这个单据的这个商品现价为hyj1
                    string sql1 = string.Format("UPDATE [xsbak] SET [现价] = '{0}' WHERE djh='{1}' and 商品名称='{2}'", hyj1, txtdjh.Text, dgvxsqd.Rows[i].Cells[0].Value);
                    SqlCommand comm1 = new SqlCommand(sql1, conn1);
                    comm1.ExecuteNonQuery();
                    sdr.Close();
                }

                conn.Close();
                conn1.Close();
            }
            else if (lbyhfs.Text.Trim() == "会员价三")
            {
                SqlConnection conn = new SqlConnection(constr);
                conn.Open();
                SqlConnection conn1 = new SqlConnection(constr);
                conn1.Open();
                for (int i = 0; i < dgvxsqd.Rows.Count; i++)
                {
                    decimal hyj1 = 0;//会员价1
                    string sqlstr = string.Format("select iname,hyj1,hyj2,hyj3 from items where iname='{0}'", dgvxsqd.Rows[i].Cells[0].Value);
                    SqlCommand comm = new SqlCommand(sqlstr, conn);
                    SqlDataReader sdr = comm.ExecuteReader();
                    while (sdr.Read())
                    {
                        hyj1 = decimal.Parse(sdr["hyj3"].ToString());
                    }
                    //修改这个单据的这个商品现价为hyj1
                    string sql1 = string.Format("UPDATE [xsbak] SET [现价] = '{0}' WHERE djh='{1}' and 商品名称='{2}'", hyj1, txtdjh.Text, dgvxsqd.Rows[i].Cells[0].Value);
                    SqlCommand comm1 = new SqlCommand(sql1, conn1);
                    comm1.ExecuteNonQuery();
                    sdr.Close();
                }

                conn.Close();
                conn1.Close();
            }
            else if (lbyhfs.Text.Trim() == "折扣")
            {
                //判断商品是否允许打折
                //之后允许打折  进行打折
                SqlConnection conn = new SqlConnection(constr);
                conn.Open();
                SqlConnection connxj = new SqlConnection(constr);
                connxj.Open();
                for (int i = 0; i < dgvxsqd.Rows.Count; i++)
                {
                    int dz = 0;
                    string ifdz =string.Format( "SELECT [isbonus] FROM [items] where iname='{0}'",dgvxsqd.Rows[i].Cells[0].Value);
                    SqlCommand comm = new SqlCommand(ifdz, conn);
                    SqlDataReader sdr = comm.ExecuteReader();
                    while (sdr.Read())
                    {
                        dz = Int32.Parse(sdr["isbonus"].ToString());
                    }
                    //判断是否打折
                    if (dz == 1)
                    {
                        //打折
                        decimal xj = decimal.Parse(dgvxsqd.Rows[i].Cells[1].Value.ToString()) * decimal.Parse(lbzl.Text);
                        //修改现价
                        string sql1 = string.Format("UPDATE [xsbak] SET [现价] = '{0}' WHERE djh='{1}' and 商品名称='{2}'", xj, txtdjh.Text, dgvxsqd.Rows[i].Cells[0].Value);
                        SqlCommand comm1 = new SqlCommand(sql1, connxj);
                        comm1.ExecuteNonQuery();
                    }
                   
                    sdr.Close();

                }
                conn.Close();
                connxj.Close();
               
            }
            SqlConnection connz = new SqlConnection(constr);
            connz.Open();
            string sql = string.Format("SELECT [商品名称],[原价]  ,[现价],[数量] ,[小计] FROM [xsbak] where [djh]='{0}'",txtdjh.Text);
            SqlDataAdapter sda = new SqlDataAdapter(sql,connz);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            dgvxsqd.DataSource = dt;
            connz.Close();
            js();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            //刷卡判断优惠方式
            //把 当前消费列表保存到数据库
            //重新计算好优惠后的数据显示到前台消费列表
            //结算后清空此表
            insertxsbak();
            chuanzhi ca = new chuanzhi();
            if (txtcardid.Text != string.Empty)
                {
                    if (ca.CheckPass(txtcardid.Text) == true)
                    {
                        //判断卡是否有密码

                        if (ca.keyong(txtcardid.Text) == true)
                        {

                            if (ca.checkpwdifnull(txtcardid.Text) == false)
                            {
                                string cardID = txtcardid.Text.ToString();//卡号或者手机号
                                if (cardID == string.Empty)
                                {
                                    MessageBox.Show("请输入正确的卡号");
                                }
                                else
                                {
                                    // chuanzhi ca = new chuanzhi();

                                    if (ca.Checkyxq(txtcardid.Text) == true)
                                    {

                                        string sql = string.Format("SELECT [mid],[mname],[mphone],[mbalance],card.cdiscount,cid FROM [member] join card on member.mcid=card.cid  where mid='{0}' or mphone='{1}'", cardID, cardID);
                                        SqlConnection conn = new SqlConnection(constr);
                                        try
                                        {
                                            conn.Open();
                                            SqlCommand comm = new SqlCommand(sql, conn);
                                            SqlDataReader sdr = comm.ExecuteReader();
                                            int cid = 0;
                                            while (sdr.Read())
                                            {
                                                lbcardID.Text = cardID;
                                                lbckr.Text = sdr["mname"].ToString();
                                                lbye.Text = sdr["mbalance"].ToString();
                                                lbphone.Text = sdr["mphone"].ToString();
                                                lbzl.Text = sdr["cdiscount"].ToString();
                                                cid =Int32.Parse( sdr["cid"].ToString());
                                            }
                                            conn.Close();
                                        SqlConnection conn1 = new SqlConnection(constr);
                                        conn1.Open();
                                        string sqlstr =string.Format( "select cid,cname,优惠方式 from card where cid={0}",cid);
                                        SqlCommand comm1 = new SqlCommand(sqlstr,conn1);
                                        SqlDataReader sdr1 = comm1.ExecuteReader();
                                        while (sdr1.Read())
                                        {
                                            lbklb.Text = sdr1["cname"].ToString();
                                            lbyhfs.Text = sdr1["优惠方式"].ToString();
                                        }
                                        }
                                        catch (Exception ex)
                                        {
                                            conn.Close();
                                            MessageBox.Show(ex.Message);
                                        }
                                    upzk();
                                    }
                                    else
                                    {
                                        MessageBox.Show("此卡已过有效期！");
                                        txtcardid.Text = null;
                                    }

                                }
                            }
                            else
                            {
                                //如果密码不为空择弹出输入密码窗体
                                chuanzhi.cardidpp = txtcardid.Text;
                                frmcheckpwd pwd = new frmcheckpwd();
                                pwd.ShowDialog();

                                //判断返回值确定密码输入情况
                                if (chuanzhi.ifpwd == 1)
                                {
                                    string cardID = txtcardid.Text.ToString();//卡号或者手机号
                                    if (cardID == string.Empty)
                                    {
                                        MessageBox.Show("请输入正确的卡号");
                                    }
                                    else
                                    {
                                        // chuanzhi ca = new chuanzhi();
                                        if (ca.CheckPass(txtcardid.Text) == true)
                                        {
                                            if (ca.Checkyxq(txtcardid.Text) == true)
                                            {

                                                string sql = string.Format("SELECT [mid],[mname],cid,[mphone],[mbalance],card.cdiscount FROM [BHairDB].[dbo].[member] join card on member.mcid=card.cid  where mid='{0}' or mphone='{1}'", cardID, cardID);
                                                SqlConnection conn = new SqlConnection(constr);
                                                try
                                                {
                                                    conn.Open();
                                                    SqlCommand comm = new SqlCommand(sql, conn);
                                                    SqlDataReader sdr = comm.ExecuteReader();
                                                int cid = 0;
                                                while (sdr.Read())
                                                    {
                                                        lbcardID.Text = cardID;
                                                        lbckr.Text = sdr["mname"].ToString();
                                                        lbye.Text = sdr["mbalance"].ToString();
                                                        lbphone.Text = sdr["mphone"].ToString();
                                                        lbzl.Text = sdr["cdiscount"].ToString();
                                                    cid = Int32.Parse(sdr["cid"].ToString());
                                                }
                                                    conn.Close();
                                                SqlConnection conn1 = new SqlConnection(constr);
                                                conn1.Open();
                                                string sqlstr = string.Format("select cid,cname,优惠方式 from card where cid={0}", cid);
                                                SqlCommand comm1 = new SqlCommand(sqlstr, conn1);
                                                SqlDataReader sdr1 = comm1.ExecuteReader();
                                                while (sdr1.Read())
                                                {
                                                    lbklb.Text = sdr1["cname"].ToString();
                                                    lbyhfs.Text = sdr1["优惠方式"].ToString();
                                                }
                                                upzk();
                                            }
                                                catch (Exception ex)
                                                {
                                                    conn.Close();
                                                    MessageBox.Show(ex.Message);
                                                }
                                            }
                                            else
                                            {
                                                MessageBox.Show("此卡已过有效期！");
                                                txtcardid.Text = null;
                                            }
                                        }
                                        else
                                        {
                                            MessageBox.Show("卡号不存在!");
                                        }
                                    }
                                }
                                else
                                {
                                    txtcardid.Text = null;
                                }
                            }

                        }
                        else
                        {
                            MessageBox.Show("此卡为不可用状态");
                        }
                    }
                    else
                    {
                        MessageBox.Show("此卡不存在");
                    }
                }
                else
                {
                    MessageBox.Show("请输入卡号！");
                }
            
        }
            

        

        private void button4_Click(object sender, EventArgs e)
        {
            frmhycz cz = new frmhycz();
            cz.ShowDialog();
            chuanzhi cz1 = new chuanzhi();
            cz1.rizhi("会员充值");
        }

    private void txtcardid_KeyDown(object sender, KeyEventArgs e)
    {
           
            chuanzhi ca = new chuanzhi();

            if (e.KeyValue == 13)
            {
                //刷卡判断优惠方式
                //把 当前消费列表保存到数据库
                //重新计算好优惠后的数据显示到前台消费列表
                //结算后清空此表
                insertxsbak();
                //chuanzhi ca = new chuanzhi();
                if (txtcardid.Text != string.Empty)
                {
                    if (ca.CheckPass(txtcardid.Text) == true)
                    {
                        //判断卡是否有密码

                        if (ca.keyong(txtcardid.Text) == true)
                        {

                            if (ca.checkpwdifnull(txtcardid.Text) == false)
                            {
                                string cardID = txtcardid.Text.ToString();//卡号或者手机号
                                if (cardID == string.Empty)
                                {
                                    MessageBox.Show("请输入正确的卡号");
                                }
                                else
                                {
                                    // chuanzhi ca = new chuanzhi();

                                    if (ca.Checkyxq(txtcardid.Text) == true)
                                    {

                                        string sql = string.Format("SELECT [mid],[mname],[mphone],[mbalance],card.cdiscount,cid FROM [member] join card on member.mcid=card.cid  where mid='{0}' or mphone='{1}'", cardID, cardID);
                                        SqlConnection conn = new SqlConnection(constr);
                                        try
                                        {
                                            conn.Open();
                                            SqlCommand comm = new SqlCommand(sql, conn);
                                            SqlDataReader sdr = comm.ExecuteReader();
                                            int cid = 0;
                                            while (sdr.Read())
                                            {
                                                lbcardID.Text = cardID;
                                                lbckr.Text = sdr["mname"].ToString();
                                                lbye.Text = sdr["mbalance"].ToString();
                                                lbphone.Text = sdr["mphone"].ToString();
                                                lbzl.Text = sdr["cdiscount"].ToString();
                                                cid = Int32.Parse(sdr["cid"].ToString());
                                            }
                                            conn.Close();
                                            SqlConnection conn1 = new SqlConnection(constr);
                                            conn1.Open();
                                            string sqlstr = string.Format("select cid,cname,优惠方式 from card where cid={0}", cid);
                                            SqlCommand comm1 = new SqlCommand(sqlstr, conn1);
                                            SqlDataReader sdr1 = comm1.ExecuteReader();
                                            while (sdr1.Read())
                                            {
                                                lbklb.Text = sdr1["cname"].ToString();
                                                lbyhfs.Text = sdr1["优惠方式"].ToString();
                                            }
                                        }
                                        catch (Exception ex)
                                        {
                                            conn.Close();
                                            MessageBox.Show(ex.Message);
                                        }
                                        upzk();
                                    }
                                    else
                                    {
                                        MessageBox.Show("此卡已过有效期！");
                                        txtcardid.Text = null;
                                    }

                                }
                            }
                            else
                            {
                                //如果密码不为空择弹出输入密码窗体
                                chuanzhi.cardidpp = txtcardid.Text;
                                frmcheckpwd pwd = new frmcheckpwd();
                                pwd.ShowDialog();

                                //判断返回值确定密码输入情况
                                if (chuanzhi.ifpwd == 1)
                                {
                                    string cardID = txtcardid.Text.ToString();//卡号或者手机号
                                    if (cardID == string.Empty)
                                    {
                                        MessageBox.Show("请输入正确的卡号");
                                    }
                                    else
                                    {
                                        // chuanzhi ca = new chuanzhi();
                                        if (ca.CheckPass(txtcardid.Text) == true)
                                        {
                                            if (ca.Checkyxq(txtcardid.Text) == true)
                                            {

                                                string sql = string.Format("SELECT [mid],[mname],cid,[mphone],[mbalance],card.cdiscount FROM [BHairDB].[dbo].[member] join card on member.mcid=card.cid  where mid='{0}' or mphone='{1}'", cardID, cardID);
                                                SqlConnection conn = new SqlConnection(constr);
                                                try
                                                {
                                                    conn.Open();
                                                    SqlCommand comm = new SqlCommand(sql, conn);
                                                    SqlDataReader sdr = comm.ExecuteReader();
                                                    int cid = 0;
                                                    while (sdr.Read())
                                                    {
                                                        lbcardID.Text = cardID;
                                                        lbckr.Text = sdr["mname"].ToString();
                                                        lbye.Text = sdr["mbalance"].ToString();
                                                        lbphone.Text = sdr["mphone"].ToString();
                                                        lbzl.Text = sdr["cdiscount"].ToString();
                                                        cid = Int32.Parse(sdr["cid"].ToString());
                                                    }
                                                    conn.Close();
                                                    SqlConnection conn1 = new SqlConnection(constr);
                                                    conn1.Open();
                                                    string sqlstr = string.Format("select cid,cname,优惠方式 from card where cid={0}", cid);
                                                    SqlCommand comm1 = new SqlCommand(sqlstr, conn1);
                                                    SqlDataReader sdr1 = comm1.ExecuteReader();
                                                    while (sdr1.Read())
                                                    {
                                                        lbklb.Text = sdr1["cname"].ToString();
                                                        lbyhfs.Text = sdr1["优惠方式"].ToString();
                                                    }
                                                    upzk();
                                                }
                                                catch (Exception ex)
                                                {
                                                    conn.Close();
                                                    MessageBox.Show(ex.Message);
                                                }
                                            }
                                            else
                                            {
                                                MessageBox.Show("此卡已过有效期！");
                                                txtcardid.Text = null;
                                            }
                                        }
                                        else
                                        {
                                            MessageBox.Show("卡号不存在!");
                                        }
                                    }
                                }
                                else
                                {
                                    txtcardid.Text = null;
                                }
                            }

                        }
                        else
                        {
                            MessageBox.Show("此卡为不可用状态");
                        }
                    }
                    else
                    {
                        MessageBox.Show("此卡不存在");
                    }
                }
                else
                {
                    MessageBox.Show("请输入卡号！");
                }



            }


        }

        private void frmxs_Activated(object sender, EventArgs e)
        {
            txtcardid.Focus();
        }
        //收款
        private void button5_Click(object sender, EventArgs e)
        {
            //chuanzhi.knye= decimal.Parse(lbye.Text);
            //判断卡内余额够不够支付本次付款
            chuanzhi.jzys= decimal.Parse(lbheji.Text);
            decimal ye = decimal.Parse(lbye.Text);
            decimal hj = decimal.Parse(lbheji.Text);
            if (ye<=0)
            {
                MessageBox.Show("余额不对请检查");
            }

            if (hj > ye)
            {
                //MessageBox.Show("卡内余额不足");
                chuanzhi.yezf = decimal.Parse(lbye.Text);
            }
            else
            {
                chuanzhi.yezf= decimal.Parse(lbheji.Text);
            }

            frmjiezhang jz = new frmjiezhang();
            jz.ShowDialog();

            if (chuanzhi.jzbs==1)
            {
                chuanzhi cz = new chuanzhi();
                cz.rizhi("销售收款");
                insertxsb();
                insertxsmx();
                // dy();
                //upkc();
                delectxsbak();
                koukuan();
                upkc();
                //dy();
            }

            ////判断卡内余额够不够本次付款

            //decimal ye = decimal.Parse(lbye.Text);
            //decimal hj = decimal.Parse(lbheji.Text);
            //if (hj > ye)
            //{
            //    MessageBox.Show("卡内余额不足");
            //}
            //else
            //{
            //chuanzhi cz = new chuanzhi();
            //cz.rizhi("销售收款");
            //insertxsb();
            //    insertxsmx();
            //    // dy();
            //    upkc();
            //    koukuan();
            //    upkc();
            //    //dy();

            //}



        }
        //清理xsbak表
        public void delectxsbak()
        {
            SqlConnection conn = new SqlConnection(constr);
            conn.Open();
            string sqlstr =string.Format( "DELETE FROM [xsbak] WHERE djh='{0}' and jsj='{1}'",txtdjh.Text, GetComputerName());
            SqlCommand comm = new SqlCommand(sqlstr,conn);
            comm.ExecuteNonQuery();
            conn.Close();
        }
        
        //扣款
        public void koukuan()
        {
            SqlConnection conn = new SqlConnection(constr);
            decimal upye = decimal.Parse(lbye.Text) - chuanzhi.yezf;
            try
            {
                conn.Open();
                string sql =string.Format( "UPDATE [member]SET [mbalance] = '{0}' WHERE mid='{1}'",upye,txtcardid.Text);
                SqlCommand comm = new SqlCommand(sql,conn);
                comm.ExecuteNonQuery();
                MessageBox.Show("扣款成功，本次消费："+chuanzhi.yezf+"元");
                dy();
                djhshuaxin();
                djh();
                shuaxin();
                conn.Close();
            }
            catch (Exception ex)
            {
                conn.Close();
                MessageBox.Show(ex.Message);
            }
        }
        //清空窗体
        public void shuaxin()
        {
            //txtcardid.Text = "";
            //lbcardID.Text = "";
            //lbckr.Text = "";
            //lbheji.Text = "";
            //lbphone.Text = "";
            //lbye.Text = "";
            //DataGridView dgv = dgvxsqd;
            //dgv.Rows.Clear();
            //txtcardid.Focus();
            this.Controls.Clear();
            InitializeComponent();
            this.txtcardid.Focus();
            djh();
            showfenlei();


        }
        //单据号
        public void djh()
        {
            string lsh = "";
            try
            {
                SqlConnection conn = new SqlConnection(constr);
                conn.Open();
                string sql = "SELECT [lsdjh] FROM [djh]";
                SqlCommand comm =new SqlCommand(sql,conn);
                SqlDataReader sdr = comm.ExecuteReader();
                while (sdr.Read())
                {
                    lsh = sdr["lsdjh"].ToString();
                }
                string bs = "LS-";
                string time = DateTime.Now.ToString("yyyy-MM-dd");
                /*
                Random ran = new Random();
                int RandKey = ran.Next(10, 999);
                */
                int djlsh =Int32.Parse( lsh )+ 1;
                siweils = string.Format("{0:00000}", djlsh);
                string djh = bs + time + "-" +siweils;
                this.txtdjh.Text = djh;
                conn.Close();
            }
            catch (Exception ex)
            {
                
                MessageBox.Show(ex.Message);
            }
        }
        //单据号+1
        public void djhshuaxin()
        {
            try
            {
                SqlConnection conn = new SqlConnection(constr);
                conn.Open();
                
                string sql =string.Format( "UPDATE [djh] SET [lsdjh] = '{0}'",siweils);
                SqlCommand comm = new SqlCommand(sql,conn);
                comm.ExecuteNonQuery();
            }
            catch { }
        }
        //插入销售表
        public void insertxsb()
        {
            SqlConnection conn = new SqlConnection(constr);
            try
            {
                conn.Open();
                string sql =string.Format("INSERT INTO [销售表]([单据号] ,[经手人] ,[单据金额]  ,[卡号] ,[单据日期],[单据备注],[班组]) VALUES('{0}','{1}','{2}' ,'{3}' ,'{4}','{5}','{6}' )", txtdjh.Text,chuanzhi.userID,lbheji.Text,txtcardid.Text, DateTime.Now.ToString("yyyy-MM-dd"),txtdjbz.Text,chuanzhi.banzu);
                SqlCommand comm = new SqlCommand(sql,conn);
                comm.ExecuteNonQuery();
                conn.Close();
            }
            catch { }
        }
        //插入销售明细
        public void insertxsmx()
        {
            SqlConnection conn = new SqlConnection(constr);
            try
            {
                conn.Open();
                
                for (int i = 0; i < dgvxsqd.Rows.Count; i++)
                {
                    string sql = string.Format("INSERT INTO [销售明细] ([单据号] ,[商品名称]  ,[销售数量] ,[销售单价] ,[销售金额] ,[经手人] ,[单据日期] ,[销售折扣] ,[会员卡号] ,[持卡人] ,[单据备注],[班组])"
                    + " VALUES('{0}' ,'{1}' ,'{2}' ,'{3}' ,'{4}' ,'{5}' ,'{6}' ,'{7}' ,'{8}' ,'{9}'  ,'{10}','{11}')", txtdjh.Text, dgvxsqd.Rows[i].Cells[0].Value, dgvxsqd.Rows[i].Cells[2].Value, dgvxsqd.Rows[i].Cells[1].Value, dgvxsqd.Rows[i].Cells[3].Value,chuanzhi.userID, DateTime.Now.ToString("yyyy-MM-dd"),lbzl.Text,txtcardid.Text,lbckr.Text,txtdjbz.Text,chuanzhi.banzu);
                    SqlCommand comm = new SqlCommand(sql,conn);
                    comm.ExecuteNonQuery();
                }
                conn.Close();
            }
            catch { }
        }
        //减少库存
        public void upkc()
        {
            SqlConnection conn = new SqlConnection(constr);
            SqlConnection conn1 = new SqlConnection(constr);
            try
            {
                
                decimal oldkc = 0;
                decimal upkc = 0;
                

                //查询库存
                for (int i = 0; i < dgvxsqd.Rows.Count; i++)
                {
                    conn.Open();
                    string sql = string.Format("SELECT  [iname],[iamount]FROM [BHairDB].[dbo].[items] where iname='{0}'", dgvxsqd.Rows[i].Cells[0].Value);
                    SqlCommand comm = new SqlCommand(sql,conn);
                    SqlDataReader sdr = comm.ExecuteReader();
                    while (sdr.Read())
                    {
                        oldkc =decimal.Parse( sdr["iamount"].ToString());
                        upkc = oldkc - decimal.Parse(dgvxsqd.Rows[i].Cells[2].Value.ToString());
                         //修改库存
                       string  sql1 =string.Format( "UPDATE [BHairDB].[dbo].[items] SET  [iamount] ='{0}' WHERE iname='{1}'",upkc, dgvxsqd.Rows[i].Cells[0].Value);
                        conn1.Open();
                        SqlCommand comm1 = new SqlCommand(sql1,conn1);
                        comm1.ExecuteNonQuery();
                        
                    }
                    conn.Close();
                    conn1.Close();

                }
                
            }
            catch { }
        }
        public void dy()
        {
            DevExpress.XtraReports.UI.XtraReport xr = new DevExpress.XtraReports.UI.XtraReport();
            xr.LoadLayout(AppDomain.CurrentDomain.BaseDirectory + @"bb\零售单.repx");
            xr.DataSource = savedy();
           // xr.ShowPreview();
            xr.Print();
        }
        public DataSet savedy()
        {
            DataSet ds = new DataSet();
            string sql =string.Format("SELECT  销售明细.[单据号] ,销售明细.[商品名称] ,销售明细.[销售数量] ,销售明细.[销售单价] ,销售明细.[销售金额] ,销售明细.[经手人],销售明细.[单据日期] ,销售明细.[销售折扣] ,销售明细.[会员卡号] ,销售明细.[持卡人] ,销售明细.[单据备注],销售表.单据号,销售表.单据备注,销售表.单据日期,销售表.单据金额 ,销售表.卡号 ,销售表.经手人 FROM [销售明细] join 销售表 on 销售明细.单据号=销售表.单据号 where 销售明细.单据号='{0}'", txtdjh.Text);
            SqlConnection conn = new SqlConnection(constr);
            try
            {
                conn.Open();
                SqlDataAdapter sda = new SqlDataAdapter(sql,conn);
                sda.Fill(ds,"销售表");
                conn.Close();
            }
            catch { }
            return ds;
        }
        //样式设置
        private void button3_Click(object sender, EventArgs e)
        {
            DevExpress.XtraReports.UI.XtraReport xr = new DevExpress.XtraReports.UI.XtraReport();
            xr.LoadLayout(AppDomain.CurrentDomain.BaseDirectory + @"bb\零售单.repx");
            xr.Report.Name = "零售单";
            xr.ShowDesignerDialog();
            xr.SaveLayout(AppDomain.CurrentDomain.BaseDirectory + @"bb\零售单.repx");
            xr.Dispose();
        }
        /// <summary>  
        /// 计算机名称  
        /// </summary>  
        /// <returns></returns>  
        public static string GetComputerName()
        {
            try
            {
                return System.Environment.GetEnvironmentVariable("ComputerName");
            }
            catch
            {
                return "unknow";
            }
        }


        private void frmxs_FormClosing(object sender, FormClosingEventArgs e)
        {

            string sqlstr =string.Format( "DELETE FROM [xsbak] WHERE jsj='{0}' and djh='{1}'",GetComputerName(),txtdjh.Text);
            SqlConnection conn = new SqlConnection(constr);
            conn.Open();
            SqlCommand comm = new SqlCommand(sqlstr,conn);
            comm.ExecuteNonQuery();
            conn.Close();
        }

        private void frmxs_FormClosed(object sender, FormClosedEventArgs e)
        {
            string sqlstr = string.Format("DELETE FROM [xsbak] WHERE jsj='{0}' and djh='{1}'", GetComputerName(), txtdjh.Text);
            SqlConnection conn = new SqlConnection(constr);
            conn.Open();
            SqlCommand comm = new SqlCommand(sqlstr, conn);
            comm.ExecuteNonQuery();
            conn.Close();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            this.Controls.Clear();
            InitializeComponent();
            this.txtcardid.Focus();
            djh();
            showfenlei();
        }
    }
}
